package com.slz3.business.module.drawManagement.draUserNumber.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.drawManagement.draUserNumber.domain.DraUserNumber;
import com.slz3.business.module.drawManagement.draUserNumber.domain.DraUserNumberFactory;
import com.slz3.business.module.drawManagement.draUserNumber.steward.DraUserNumberSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class DraUserNumberServiceImpl implements DraUserNumberService {

    @Override
    public DraUserNumber save(DraUserNumber draUserNumber) {
        return Repositorys.draUserNumber.save(DraUserNumberFactory.initBaseFields(draUserNumber));
    }

    @Override
    public Page<DraUserNumber> page(MultiValueMap<String, String> params) {
        Page<DraUserNumber> draUserNumberPage = Repositorys.draUserNumber.findAll(
                DraUserNumberSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return draUserNumberPage;
    }

    @Override
    public DraUserNumber modify(DraUserNumber draUserNumberNew) {
        return Repositorys.draUserNumber.saveAndFlush(DraUserNumberFactory.initBaseFields(draUserNumberNew));
    }

    @Override
    public Optional<DraUserNumber> single(UUID uuid) {
        return Repositorys.draUserNumber.findById(uuid);
    }

    @Override
    public List<DraUserNumber> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.draUserNumber.findAllById(uuids);
    }

    @Override
    public List<DraUserNumber> delete(List<DraUserNumber> draUserNumberList) {
        draUserNumberList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            DraUserNumberFactory.initBaseFields(item);
        });
        return Services.draUserNumberTransactional.batchModify(draUserNumberList);
    }

}
